<?php
/**
 * 插件模型
 * @author 楚羽幽 <Name_Cyu@Foxmail.com>
 */
namespace Admin\Model;
use Think\Model;
class PlugsModel extends Model
{
    // 模型主表
    protected $tableName = 'Plugs';
    /**
     * [$_validate 自动验证]
     * @var array
     */
    protected $_validate = array(
        // array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
        array('name','require','插件标识必须填写！',1,'',3),
        array('name','/^[a-zA-Z]+$/i','插件标识必须为英文字母',1,'regex',3),
        array('title', 'require', '插件名称必须填写！', 1,'', 3),
        array('version', 'require', '插件版本必须填写！', 1,'', 3),
        array('email', 'require', '作者邮箱必须填写！', 1,'', 3),
        array('author', 'require', '插件作者必须填写！', 1,'', 3),
        array('description', 'require', '插件描述必须填写！', 1,'', 3),
    );


    /**
     * [$_auto 自动完成]
     * @var array
     */
    protected $_auto = array(
    );

    /**
     * 获取插件列表
     * @param string $addon_dir
     */
    public function getAddonList($addon_dir = '')
    {
        if(!$addon_dir)
            $addon_dir = UNION_ADDON_PATH;
        $dirs = array_map('basename',glob($addon_dir.'*', GLOB_ONLYDIR));
        if($dirs === FALSE || !file_exists($addon_dir))
        {
            $this->error = '插件目录不可读或者不存在';
            return FALSE;
        }

        $addons         =   array();
        $where['name']  =   array('in',$dirs);
        $list           =   $this->where($where)->field(true)->select();

        foreach($list as $addon)
        {
            $addon['uninstall']     =   0;
            $addons[$addon['name']] =   $addon;
        }

        foreach ($dirs as $value)
        {
            if(!isset($addons[$value]))
            {
                $class = get_addon_class($value);

                if (!class_exists($class))
                {
                    // 实例化插件失败忽略执行
                    \Think\Log::record('插件' . $value . '的入口文件不存在！');
                    continue;
                }

                $obj    =   new $class;
                $addons[$value] = $obj->info;
                
                if ($addons[$value])
                {
                    $addons[$value]['uninstall'] = 1;
                    $addons[$value]['status'] = 99;
                }
            }
            else
            {
                //todo 已安装的
            }
        }
        int_to_string($addons, array('status'=>array(0=>'禁用', 1=>'启用', null=>'未安装')));
        $addons = list_sort_by($addons,'uninstall','desc');
        return $addons;
    }

    /*------------------------------------------属性定义--------------------------------------------------*/

    /**
     * 获得插件对象
     * @param $addon 插件名
     * @return mixed;
     */
    public function getAddonObj($addon)
    {
        $classFile = UNION_ADDON_PATH . $addon . '/' . $addon . 'Addon.class.php';
        if (!is_file($classFile)) return false;
        require_cache($classFile);
        $class = $addon . 'Addon'; //类名
        if (!class_exists($class))
        {
            return false;
        }
        return new $class;
    }

    /**
     * [getAdminList 获取已安装插件，并有后台面板插件]
     * @return [type] [description]
     */
    public function getAdminList()
    {
        $admin = array();
        $db_addons = $this->where("status=1 AND has_adminlist=1")->select();
        if($db_addons)
        {
            foreach ($db_addons as $value)
            {
                $admin[] = array('title'=>$value['title'],'name'=>$value['name'],'url'=> U('adminList', array('name'=>$value['name'])),'description'=>$value['description'],'author'=> $value['author'],'version'=> $value['version']);
            }
        }
        return $admin;
    }
}
